//
//  ViewController.m
//  UI10-作业
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableDictionary *dataDic;
@property (nonatomic,retain)NSMutableDictionary *quData;
@property (nonatomic,retain)NSMutableArray *quArray;
@property (nonatomic,retain)NSArray *sortArray;
@property (nonatomic,retain)NSIndexPath *indexpath;

@end

@implementation ViewController
-(void)dealloc{
    [_tableView release];
    [_dataDic release];
    [_sortArray release];
    [_indexpath release];
    [_quData release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"通讯录";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.dataDic = [[NSMutableDictionary alloc]init];
    self.sortArray = [NSArray array];
    [self handleData];
    [self createView];
}

- (void)handleData{    //{ A:[{},{}], B:[], C:[], D:[] }
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contact" ofType:@".plist"];
    self.dataDic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    self.sortArray = [self.dataDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
}

-(void)createView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;  //分割线消失
}

//分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sortArray.count;
}
//头部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.sortArray[section];
}
//分区对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *dataArray = [self.dataDic objectForKey:self.sortArray[section]];
    return dataArray.count;
}
//侧边栏
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.sortArray;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    self.quArray = [self.dataDic objectForKey:self.sortArray[indexPath.section]];
    self.quData = self.quArray[indexPath.row];
    cell.textLabel.text = [self.quData objectForKey:@"name"];
    cell.imageView.image = [UIImage imageNamed:[self.quData objectForKey:@"header"]];
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 22;
    return cell;
}
//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *secView = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secView animated:YES];
    NSArray *array = [self.dataDic objectForKey:self.sortArray[indexPath.section]];
    secView.titleString = [array[indexPath.row] objectForKey:@"name"];
    secView.QQ = [array[indexPath.row] objectForKey:@"QQ"];
    secView.number = [array[indexPath.row] objectForKey:@"number"];
    secView.picName = [array[indexPath.row] objectForKey:@"header"];
    secView.delegate = self;
    [secView release];
    self.indexpath = indexPath;
}

- (void)sendValue:(NSMutableDictionary *)dic{
    self.quArray = [self.dataDic objectForKey:self.sortArray[self.indexpath.section]];
    self.quArray[self.indexpath.row] = dic;
    [self.tableView reloadData];
}























































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
